package com.ysd.lis.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysd.common.Result;
import com.ysd.lis.request.CommonPageDto;
import com.ysd.lis.entity.WkSchduleType;
import com.ysd.lis.mapper.SchduleTypeMapper;
import com.ysd.lis.service.SchduleTypeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Slf4j
@Service
public class SchduleTypeServiceImpl extends ServiceImpl<SchduleTypeMapper, WkSchduleType> implements SchduleTypeService {

    @Autowired
    SchduleTypeMapper schduleTypeMapper;

    @Override
    public Result addSchduleType(WkSchduleType wkSchduleType){
        int i = schduleTypeMapper.insert(wkSchduleType);
        if(i == 1){
            return Result.succ(1,"保存成功", wkSchduleType);
        }else{
            return Result.fail(400,"添加失败","");
        }
    }

    @Override
    public Result deleteSchduleType(String id){
        WkSchduleType wkSchduleType =  schduleTypeMapper.selectById(id);
        wkSchduleType.setDelFlag(1);
        int i = schduleTypeMapper.updateById(wkSchduleType);
        if(i > 0){
            return Result.succ(1,"删除成功","");
        }else{
            return Result.fail(400,"删除失败","");
        }
    }

    @Override
    public Result editSchduleType(WkSchduleType wkSchduleType){
        int i = schduleTypeMapper.updateById(wkSchduleType);
        if(i>0){
            return Result.succ(1,"修改成功", wkSchduleType);
        }else{
            return Result.fail(400,"修改失败","");
        }
    }

    @Override
    public Result findSchduleTypePage(CommonPageDto pageParam){
        LambdaQueryWrapper<WkSchduleType> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(WkSchduleType::getDelFlag,0);
        if(StringUtils.isNotBlank(pageParam.getSeachValue())){
            lambdaQueryWrapper.and(wrapper -> wrapper.like(WkSchduleType::getName, pageParam.getSeachValue()).or()
                    .eq(WkSchduleType::getCode,pageParam.getSeachValue()));
        }
        lambdaQueryWrapper.orderByAsc(WkSchduleType::getSeq);
        List<WkSchduleType> wkSchduleTypes = schduleTypeMapper.selectList(lambdaQueryWrapper);
        return Result.succ(1,"查询成功",wkSchduleTypes);
    }

}
